define(function(require, exports, module) {
	//lib工具包
	require('resources/js/jquery.json-2.4.min.js');
	require('resources/js/avalon-2.0.js');
	
	//公用逻辑
	var resConstants=require('resources/js/rescatalogMix/res_constants.js');
	
	var ResPublishEditor=require('resources/template/resInfo/resPublish_editor.js');
	var resPublishEditorTpl=require('text!resources/template/resInfo/resPublish_editor_tpl.html');
	
	//模块代码
	require('resources/js/GooFlow-0.8/GooFlow.js');
	var FlowChart=require('resources/js/rescatalogMix/FlowChart.js');
	
	var ResPublishDetail=function(params){
		this.curResId=-1;
		//默认值
		this.resInfos={};	//<resId,resInfo>
		this.resInfoCmp={};	//<resId,ResInfoSchemaOperate,ServiceOperatePlugin>
		//配置
		$.extend(true,this,params);
		
		this.init();
	};
	
	ResPublishDetail.prototype={
		init:function(){
			this.render();
			this.bind();
			this.sync();
		},
		render:function(){
			this.$div=$('#res-edit');
			this.resPublishDiv=$('.resPublish-property',this.$div);
			$(resPublishEditorTpl).prependTo(this.resPublishDiv);
			
			this.flowChart=new FlowChart({
				dataUrl:this.getUrl('workflow')+this.instanceId,
				id:'workFlow',
				curNodeName:this.curTaskNode
			});
		},
		bind:function(){
			var that=this;
			$('#backWard').on('click',function(){
				jump(that.getUrl('list'),that.listParam);
			});
		},
		sync:function(){
			var that=this;
			$.ajax(this.getUrl(this.status!='0'?'loadVerify':'loadDraft'),{
				data:{
					resPublishId:this.resPublishId,
					status:this.status,
					resId:this.resId,
					wfVersionId:this.wfVersionId,
					instanceId:this.instanceId,
					taskId:this.taskId
				},
				async:false,
				type:'POST',
				dataType:'json'
			}).done(function(data){
				that.resPublish=data;
			}).fail(function(){});
			this.resPublish.userName=this.userName;
			//主表单填充
			//使用avalon填充
			this.vm = avalon.define($.extend(true,{
                $id: "resPublishVM",
                step:this.status!='0'?'verify':'draft'
            },this.resPublish));//联合资源的vm
			avalon.scan(this.$div.get(0),this.vm);
			//初始化 子组件
			this.resPublishEditor=new ResPublishEditor(this.vm,this.resPublishDiv,this.getCmpConfig());
			this.resPublish.items&&this.resPublish.items.length>0&&
			$.each($.map(this.resPublish.items,function(ele){return ele.resInfo}),function(index,resInfo){
				if(resInfo.resType==1)
					that.addSchemaResInfo(resInfo);
				else if(resInfo.resType==2)
					that.addServiceResInfo(resInfo);
			});
		},
		addSchemaResInfo:function(resInfo){
			var ResInfoSchemaOperate=require('resources/template/resInfo/resInfo_schema_operate.js'),
				resId=this.curResId--,
				$formContainer=$('#formContainer');
			this.resInfos[resId]=resInfo
			this.resInfoCmp[resId]=new ResInfoSchemaOperate(resInfo,$formContainer,this.getCmpConfig());
		},
		getCmpConfig:function(){
			return {
				readOnly:true,
				baseUrl:this.baseUrl,
				step:this.status!='0'?'verify':'draft',
				selfDelete:false
			};
		},
		addServiceResInfo:function(resInfo){
			var ServiceOperatePlugin=require('resources/template/resInfo/resInfo_service_editor.js'),
				resId=this.curResId--,
				$formContainer=$('#formContainer');
			this.resInfos[resId]=resInfo;
			this.resInfoCmp[resId]=new ServiceOperatePlugin(resInfo,$formContainer,this.getCmpConfig());
		},
		urls:{
			list:'/resPublishX/main.jhtml',
			loadVerify:'/resPublish/loadResPublishVerify.jhtml',
			loadDraft:'/resPublish/loadResPublish.jhtml',
			workflow:'/facade/workflowXML.jhtml?instanceId='
		},
		getUrl:function(action){
			if(this.urls[action])
				return this.baseUrl+this.urls[action];
			else 
				return this.baseUrl+action;
		}
	};
	
	return  ResPublishDetail;
});